﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.Goods
{
    /// <summary>
    /// 商品信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ProductDisplayServices : ApplicationService, IProductDisplayServices
    {
        /// <summary>
        /// 商品仓储
        /// </summary>
        IRepository<GoodsInfor> goodsRepository;
        /// <summary>
        /// 商品类型仓储
        /// </summary>
        IRepository<GoodsTypeInfor> goodsTypeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="goodsRepository">商品存给</param>
        /// <param name="goodsTypeRepository">商品类型仓储</param>
        public ProductDisplayServices(IRepository<GoodsInfor> goodsRepository, IRepository<GoodsTypeInfor> goodsTypeRepository)
        {
            this.goodsRepository = goodsRepository;
            this.goodsTypeRepository = goodsTypeRepository;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> DeleteProductAsync(List<Guid> Ids)
        { 
             await goodsRepository.DeleteAsync(x => Ids.Contains(x.Id));
            return ApiResult.Ok();
        }
        /// <summary>
        /// 小程序商品列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("GetProductAsync")]
        public async Task<ApiResult> GetProductAsync(GetProducDTO request)
        {
            var product = await goodsRepository.GetQueryableAsync();
            if (!string.IsNullOrEmpty(request.GoodsName))
            {
                product = product.Where(x => x.Name.Contains(request.GoodsName));
            }
            if(request.GoodsType == 1)
            {
                product = product.Where(x => x.TypeName == "普通商品");
            }
            if (request.GoodsType == 2)
            {
                product = product.Where(x => x.TypeName == "现金券");
            }
            if (request.GoodsType == 3)
            {
                product = product.Where(x => x.TypeName == "计次商品");
            }
            return ApiResult.Ok(product.ToList());
        }

        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("FindProductAsync")]
        public async Task<ApiResult> FindProductAsync(Guid? id)
        {
            var product = await goodsRepository.FirstOrDefaultAsync(x => x.Id == id);
            return ApiResult.Ok(product);
        }

        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="goodsRepository">商品残存给</param>
        [HttpGet]
        public async Task<ApiResult> GetProductsAsync(GetProductsRequest request)
        {
            var products = (await goodsRepository.GetQueryableAsync());
            var goodsType = products.OrderByDescending(x => x.CreationTime);
            if (request.Sellout == true)
            {
                goodsType = (IOrderedQueryable<GoodsInfor>)goodsType.Where(x => x.Remaining == x.Actual);
            }
            if (request.IsShelves.HasValue)
            {
                goodsType = (IOrderedQueryable<GoodsInfor>)goodsType.Where(x => x.IsShelves == request.IsShelves.Value);
            }
            if (!string.IsNullOrEmpty(request.Name))
            {
                goodsType = (IOrderedQueryable<GoodsInfor>)goodsType.Where(x => x.Name.Contains(request.Name));
            }
            return ApiResult.Ok(goodsType.ToList());
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="productDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> InsertProductAsync(ProductDto productDto)
        {
            var entity = ObjectMapper.Map<ProductDto, GoodsInfor>(productDto);
            await goodsRepository.InsertAsync(entity);
            return ApiResult.Ok();
        }
        /// <summary>
        /// 批量上下架
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> LoadingUnloadingtAsync(List<Guid> Ids)
        {
            var list = await goodsRepository.GetListAsync(x => Ids.Contains(x.Id));
            list.ForEach(x => x.IsShelves = !x.IsShelves);
            return ApiResult.Ok();
        }

        /// <summary>
        /// 上架
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [HttpPut("GetProductByCategoryAsync")]
        public async Task<ApiResult> GetProductByCategoryAsync(GetProductByCategoryDTO Id)
        {
           var list = await goodsRepository.GetListAsync(x => x.Id == Id.Id);
            if(list.Count() == 0)
            {
                return ApiResult.Ok("没有找到该商品");
            }
            else
            {
                list.ForEach(x => x.IsShelves = !x.IsShelves);
                await goodsRepository.UpdateAsync(list.FirstOrDefault());
                return ApiResult.Ok();
            }
            
        }
        /// <summary>
        /// 更新商品
        /// </summary>
        /// <param name="productDto"></param>
        /// <returns></returns>
        [HttpPut("UpdateProductAsync")]
        public async Task<ApiResult> UpdateProductAsync(UpdateProductDto productDto)
        {
            var entity = ObjectMapper.Map<UpdateProductDto, GoodsInfor>(productDto);
            await goodsRepository.UpdateAsync(entity);
            return ApiResult.Ok();
        }

       
        /// <summary>
        /// 更新库存
        /// </summary>
        /// <param name="id"></param>
        /// <param name="stock"></param>
        /// <returns></returns>
        [HttpPut("UpdateStockAsync")]
        public async Task<ApiResult> UpdateStockAsync(UpdateStockRequest request)
        {
            var entity = (await goodsRepository.GetQueryableAsync()).Where(x => x.Id == request.Id);
            if (entity.Count() == 0)
            {
                return ApiResult.Fail("没有找到该商品");
            }
            else
            {
                entity.FirstOrDefault().Remaining = request.Stock;
                await goodsRepository.UpdateAsync(entity.FirstOrDefault());
                return ApiResult.Ok();
            }
        }

        /// <summary>
        /// 更改排序
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("GetProductByKeywordAsync")]
        public async Task<ApiResult> GetProductByKeywordAsync(UpdateInStockRequest request)
        {
            var entity = (await goodsRepository.GetQueryableAsync()).Where(x => x.Id == request.Id);
            if (entity.Count() == 0)
            {
                return ApiResult.Fail("没有找到该商品");
            }
            else
            {
                entity.FirstOrDefault().DateSort = request.Sorts;
                await goodsRepository.UpdateAsync(entity.FirstOrDefault());
                return ApiResult.Ok();
            }
        }
    }
}
